﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionsLib.Dictionary
{
    public static class DictionaryExtensionsCaseInsensitive
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static IDictionary<string, string> ToUpperCase(this IDictionary<string, string> dictionary)
        {
            var dictionaryTemp = new Dictionary<string, string>();
            foreach (string name in dictionary.Keys)
            {
                string upperName = name.ToUpper();
                if (dictionaryTemp.ContainsKey(upperName))
                {
                    throw new Exception("Inserting the upper-representation of key=[" + name + "] conflicts with previously inserted key !");
                }
                else
                {
                    dictionaryTemp.Add(upperName, dictionary[name]);
                }
            }

            dictionary.Clear();
            foreach (var item in dictionaryTemp)
            {
                dictionary.Add(item);
            }
            return dictionary;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static IDictionary<string, TValue> ToCaseInsensitive<TValue>(this IDictionary<string, TValue> dictionary)
        {
            return new Dictionary<string, TValue>(dictionary, StringComparer.InvariantCultureIgnoreCase);
        }


      
    }
}
